package com.uchengwang.ui.cm_ui.me.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.me.bean.LinkAccountBean;
import com.uchengwang.ui.utils.SwitchAccountUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/12/4.
 */

public class LinkAdapter extends RecyclerView.Adapter<LinkAdapter.LinkVH>{
    private Context context;
    private List<LinkAccountBean> list = new ArrayList<>();
    public LinkAdapter(Context context){
        this.context = context;

    }

    public void setData(List<LinkAccountBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<LinkAccountBean> getData(){
        return list;
    }

    @Override
    public LinkVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_my_account, parent, false);
        return new LinkVH(view);
    }

    @Override
    public void onBindViewHolder(LinkVH holder, int position) {
        final LinkAccountBean bean = list.get(position);

        if (TextUtils.isEmpty(bean.getFakerUserAvatar())){
            ImageManager.getInstance().loadImage(context, holder.head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.head, bean.getFakerUserAvatar(), ImageManager.ImageType.CIRCLE);
        }

        holder.name.setText(bean.getFakerNickName());

        final boolean isCurrent = (bean.getFakerUid() == UCommonUtil.getUtil().getId());
        if (isCurrent){
            holder.admin.setVisibility(View.VISIBLE);
            holder.admin.setText("在线");
            holder.admin.setTextColor(context.getResources().getColor(R.color.ucw_color));
        }else {
            holder.admin.setVisibility(View.GONE);
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isCurrent){
                    UCommonUtil.getUtil().showToast(context, "当前正在使用");
                }else {
                    //切换关联账号
                    SwitchAccountUtils.getUtils().switchLink(context, bean);
                }

            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class LinkVH extends RecyclerView.ViewHolder {
        private View itemView;
        private ImageView head;
        private TextView name, admin, noRead, account_sign;

        public LinkVH(View view) {
            super(view);
            AutoUtils.autoSize(view);

            itemView = view;
            head = (ImageView) view.findViewById(R.id.account_head);
            name = (TextView) view.findViewById(R.id.account_name);
            admin = (TextView) view.findViewById(R.id.account_admin);
            account_sign = (TextView) view.findViewById(R.id.account_sign);
            account_sign.setVisibility(View.GONE);
            noRead = (TextView) view.findViewById(R.id.account_noRead);
        }
    }
}
